#!/usr/bin/env bash
# 获取命令行参数
imageName=$1
imageInfoArr=($(echo $imageName | tr '/' ' '))
harbor_url=${imageInfoArr[0]}
harbor_project_name=${imageInfoArr[1]}
imageTagArr=($(echo ${imageInfoArr[2]} | tr ':' ' '))
project_name=${imageTagArr[0]}
tag=${imageTagArr[1]}

echo "镜像名称------$imageName"
# 查询容器是否存在，存在则删除
containerId=`docker ps -a | grep -w ${harbor_url}/${harbor_project_name}/${project_name} | awk '{print $1}'`
echo "容器ID------$containerId"
if [ "$containerId" != "" ] ; then
#停掉容器
docker stop $containerId
#删除容器
docker rm $containerId
echo "成功删除容器------$containerId"
fi
# 查询镜像是否存在，存在则删除
imageId=`docker images | grep -w $project_name | awk '{print $3}'`
echo "镜像ID------$imageId"
if [ "$imageId" != "" ] ; then
# 删除镜像
docker rmi -f $imageId
echo "成功删除镜像------$imageId"
fi
# 登录Harbor私服
docker login -u admin -p hera@321 $harbor_url
# 下载镜像
docker pull $imageName
# 启动容器
docker run --name $project_name \
-p 8000:80 \
-p 8443:443 \
-v /opt/docker/etc/$project_name/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf \
-v /opt/docker/etc/$project_name/iot-channel.conf:/usr/local/openresty/nginx/conf/conf.d/iot-channel.conf \
-v /opt/docker/log/$project_name:/home/app/log \
-v /etc/localtime:/etc/localtime:ro \
-di $imageName